﻿using MT.CustomUserControl.Models;
using MT.CustomUserControl.ViewModels;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MT.CustomUserControl.Views
{
    /// <summary>
    /// ChronicUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class ChronicUserControl : UserControl
    {
        private ChronicUcViewModel chronicUcViewModel;

        public ChronicUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null)
            {
                chronicUcViewModel = this.DataContext as ChronicUcViewModel;
            }
        }

        private void TextBox_Condition_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (TextBox_Condition.Text.Trim().Length > 0)
            {
                var tempList = chronicUcViewModel.ChronicDiseaseList.Where(chronic => chronic.Name.Contains(TextBox_Condition.Text) || chronic.ShortName.Contains(TextBox_Condition.Text.ToUpper())).ToList();

                chronicUcViewModel.ChronicDiseases.Clear();
                foreach (var temp in tempList)
                {
                    temp.SelectedState = false;
                    chronicUcViewModel.ChronicDiseases.Add(temp);
                }

                //检索出已经选中的项
                foreach (var chronicModel in chronicUcViewModel.ChronicDiseases)
                {
                    foreach (var selectedChronic in chronicUcViewModel.SelectedChronicList)
                    {
                        if (chronicModel.Name == selectedChronic.ItemContent)
                        {
                            chronicModel.SelectedState = true;
                            break;
                        }
                    }
                }

                chronicUcViewModel.ChronicListVisibility = Visibility.Visible;
                Popup_ChronicList.IsOpen = true;
            }
            else
            {
                chronicUcViewModel.ChronicListVisibility = Visibility.Collapsed;
                Popup_ChronicList.IsOpen = false;
            }
        }

        private void TextBox_Condition_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return && !string.IsNullOrEmpty(TextBox_Condition.Text))
            {
                var tempList = chronicUcViewModel.SelectedChronicList.Where(temp => temp.ItemContent == TextBox_Condition.Text).ToList();
                if (tempList.Count == 0)
                {
                    ExtendSelectedItemMdoel extendSelectedItemMdoel = new ExtendSelectedItemMdoel();
                    extendSelectedItemMdoel.ItemContent = TextBox_Condition.Text;
                    extendSelectedItemMdoel.DeleteAction = chronicUcViewModel.DeleteSelectedChronicActionFunc;
                    chronicUcViewModel.SelectedChronicList.Add(extendSelectedItemMdoel);

                    TextBox_Condition.Text = string.Empty;
                    e.Handled = true;
                }
                else
                {
                    MessageBox.Show("当前慢性病已经添加！", "提示");
                }

                if (chronicUcViewModel.SelectedChronicList.Count > 0)
                    chronicUcViewModel.SelectedChronicVisibility = Visibility.Visible;
                else
                    chronicUcViewModel.SelectedChronicVisibility = Visibility.Collapsed;
            }
        }

        private void ListBox_ConditionalChronic_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ListBox_ConditionalChronic.SelectedIndex > -1)
            {
                ChronicModel selectedChronic = chronicUcViewModel.ChronicDiseases[ListBox_ConditionalChronic.SelectedIndex];

                var tempList = chronicUcViewModel.SelectedChronicList.Where(temp => temp.ItemContent == selectedChronic.Name).ToList();
                if (tempList.Count == 0)
                {
                    ExtendSelectedItemMdoel extendSelectedItemMdoel = new ExtendSelectedItemMdoel();
                    extendSelectedItemMdoel.ItemContent = selectedChronic.Name;
                    extendSelectedItemMdoel.DeleteAction = chronicUcViewModel.DeleteSelectedChronicActionFunc;
                    chronicUcViewModel.SelectedChronicList.Add(extendSelectedItemMdoel);
                    chronicUcViewModel.SelectedChronicListChangedAction?.Invoke();
                }
                else
                {
                    MessageBox.Show("当前慢性病已经添加！", "提示");
                }

                TextBox_Condition.Text = string.Empty;

                if (chronicUcViewModel.SelectedChronicList.Count > 0)
                    chronicUcViewModel.SelectedChronicVisibility = Visibility.Visible;
                else
                    chronicUcViewModel.SelectedChronicVisibility = Visibility.Collapsed;
            }
        }

        public void SetPopup_ChronicList_Close()
        {
            if(Popup_ChronicList.IsOpen)
                Popup_ChronicList.IsOpen = false;
        }
    }
}
